import ProductModel from "../../../models/Product";
import Crypt from "../../../utils/Crypt";
import config from "../../../config/config";

const productModel = new ProductModel()

Page({
  data: {
    page: 0,
    pageSize: 10,
    list: [],
    busy: false,
    supplier_id: 0,
    supplier: {},
    baseUrl: config.app_base_url,
    wx: false
  },

  onLoad: function (options) {

    const wx = productModel.getWxSet()
    if (wx) {
      this.setData({ wx: true })
    }

    this.setData({ supplier_id: options.id })
    this.getShop()
    this._getList()
  },

  // 商家资料
  getShop() {
    productModel.getSupplierDesc({ id: this.data.supplier_id }).then(response => {
      let { data } = response
      data = Crypt.parse(data)
      this.setData({
        supplier: {...data}
      })
      // console.log(this.data.supplier)
    })
  },

  onReachBottom() {
    this._getList(true)
  },

  _getList(append = false) {
    if (this.data.busy === false) {
      this.setData({
        busy: true,
        page: this.data.page + 1
      })
      productModel.getSupplierProduct({ ...this.data }).then(response => {
        let { data } = response
        data = Crypt.parse(data)
        if (data.list.length === 0) {
          this.setData({ busy: true })
        } else {
          this.setData({ busy: false })
          if (append) {
            this.setData({ list: this.data.list.concat(data.list) })
          } else {
            this.setData({ list: data.list })
          }
        }
      })
    }
  }
});
